use strict;

# an array which indexes all error strings with the corresponding error code

our @quickref = (  "",							#  0
		   "Multipost",						#  1
		   "User's access rights exceeded",			#  2
		   "Too many groups",					#  3
		   "Too many groups in Followup-To",			#  4
		   "Headers too large",					#  5
		   "Body too large",					#  6
		   "Total article too large",				#  7
		   "Invalid control message",				#  8
		   "Group closed",					#  9
		   "Followup forbidden",				# 10
		   "External followup is forbidden",			# 11
		   "Approved header is forbidden",			# 12
		   "Re: inside Subject without References",		# 13
		   "Too many quoted lines",				# 14
		   "Too many empty lines",				# 15
		   "Too many blank lines",				# 16
		   "Too many email addresses",				# 17
		   "Too few valid lines",				# 18
		   "Too long body line",				# 19
		   "UUEncode is forbidden",				# 20
		   "YENC is forbidden",					# 21
		   "Message too old",					# 22
		   "Message posted in the future",			# 23
		   "Posting not allowed in virgin groups",		# 24
		   "Posting not allowed in empty groups",		# 24

		);


our @twoleveltlds;
our $delayederror;
our %active;

sub load_two_level_tlds($) {
	my $file = $_[0];
	&log("debug", "Loading a list of all known second level domains, file: $file");
	unless ( open(FILE, "$file") ) {
		&log("crit", "Unable to load two level tlds file $file, aborting");
		return 1;
	}

	while ( my $line = <FILE> ) {
		chop $line;
		push(@twoleveltlds,$line);
	}
	close FILE;
	return 0;

}

sub get_etcdir($$) {
	my $base = $_[0];
	my $usr  = $_[1];	
	my $temp;

# Step 1: Directory $base/nouser/

	if (length($usr)==0) {
		$temp = "$base/nouser/";
		if (-d $temp) {
                	&log("debug", "Found a valid configuration directory for null user: $temp");
                	return $temp;
        	} else {  # Or default: note that nouser must be executed before per user check because $user eq "" when unset and $base exists
				$temp = "$base/default/";
			        if (-d $temp) {
                			&log("debug", "Found a valid default configuration directory: $temp");
                			return $temp;
        			} else {
					&log("crit", "Critical Failure: needed configuration directory not found: user null, neither /nouser/ directory nor default configuration directory" );
					return undef;
				}
			}
		}


# Step 2: Directory $base/$user/

	$temp = "$base/$usr/";
	if (-d $temp) {
		&log("debug", "Found a valid per user configuration directory: $temp");
		return $temp;
	}	

# Step 3: Directory $base/default/
	$temp = "$base/default/";
        if (-d $temp) {
                &log("debug", "Found a valid default configuration directory: $temp");
                return $temp;
        }

	&log("crit", "Critical Failure: needed configuration directory not found! User: $usr, basedir $base");
	return undef;
}

sub load_active($) {
	my $file = $_[0];
	&log("debug", "Loading active file: $file");
	unless ( open(FILE, "$file") ) {
		&log("crit", "Unable to load active file: $file, aborting");
		return 1;
	}

	my $total  = "0";
	my $virgin = "0";
	my $empty  = "0";
	my $totart = "0";
	

	while ( my $line = <FILE> ) {
		my ($name,$major,$minor,$flag) = split(/[\ |\t]+/, $line );
		chop $flag;
		$total++;
		if ($major==0){
			$virgin++;
			$active{$name} = -1;
		}
		my $articles = $major - $minor;
		if ($articles==0){
			$empty++;
		}
		$active{$name} = $articles;
		$totart += $articles;
	}

	close FILE;
  
	my $populated = $total - ($virgin+$empty);
	&log("debug", "Active: $file: total $total, virgin $virgin, empty $empty, populated $populated groups, total artcles $totart");
	return 0;
}


1;
